package com.zhxy.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.zhxy.manager.dao.CanteenDao;
import com.zhxy.manager.entity.Canteen;
import com.zhxy.manager.service.intf.CanteenService;
import com.zhxy.manager.vo.PageBean;
import com.zhxy.manager.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @Author: 花落云溪醉梦
 * @Date: 2022年03月31 20:20
 * @ClassName:
 * @Declare:
 */
//
@Service
public class CanteenServiceImpl implements CanteenService {
    @Autowired
    private CanteenDao dao;

    @Override
    public R save(Canteen canteen) {
        //执行新增操作
        if (dao.add(canteen) > 0) {
            //成功
            return R.ok();
        } else {
            //失败
            return R.fail();
        }
    }

    @Override
    public PageBean queryPage(int page, int limit) {
//        1.分页
        PageHelper.startPage(page, limit);
//        2.获取分页结果
        PageInfo<Canteen> pageInfo = new PageInfo<>(dao.selectAll());
//        3.返回分页的数据
        return PageBean.bean(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public R del(int id) {
        if (id > 0) {

            if (dao.del(id) > 0) {
                return R.ok();
            }
        }
        return R.fail();
    }

    @Override
    public R delBatch(int[] ids) {
        if (ids != null && ids.length > 0) {
            if (dao.delBath(ids) > 0) {
                return R.ok();
            }
        }
        return R.fail();
    }
//    条件查询
    @Override
    public PageBean queryPageWhere(String name, int flavour, int page, int limit) {

        if (!StringUtils.hasLength(name)) {
            name = null;
        } else {
            name = "%"+ name +"%"; //拼接上模糊查询
        }

        PageHelper.startPage(page,limit);
        PageInfo<Canteen> pageInfo = new PageInfo<>(dao.selectWhere(name,flavour));
        return PageBean.bean(pageInfo.getTotal(), pageInfo.getList());
    }
//      修改
    @Override
    public R update(Canteen canteen) {
        if (dao.update(canteen)>0){
            return R.ok();
        }else {
            return R.fail();
        }
    }


}
